package kibahed.uni.mdp.visualisation;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;
import javax.swing.plaf.basic.BasicBorders.RadioButtonBorder;

public class KBLocalPanel extends JPanel {
	
	public static final char UP = (char)(9650);
	public static final char DOWN = (char)(9660);
	public static final char LEFT = (char)9668;
	public static final char RIGHT = (char)9658;
	
	public static final int PANEL_HEIGHT = 150;
	public static final int PANEL_WIDTH = 150;
	public static final int PANEL_THICKNESS = 20;
	
	JPanel topPanel = new JPanel();
	JPanel botPanel = new JPanel();
	JPanel leftPanel = new JPanel();
	JPanel rightPanel = new JPanel();
	JPanel midPanel = new JPanel();
	
	JLabel topLabel = new JLabel();
	JLabel botLabel = new JLabel();
	JLabel leftLabel = new JLabel();
	JLabel rightLabel = new JLabel();
	
	private double value = 0.0;
	private double noise = 0.1;
	private int nextAction = 0;
	private double reward =0;
	
	
	
	JTextField rewardField = new JTextField(""+reward);
	JLabel valueLabel = new JLabel(""+value);
	JTextField noiseField = new JTextField(""+noise);
	JButton startButton = new JButton("start");
	
	
	
	
	public KBLocalPanel(JRadioButton rb){
		this.setLayout(new BorderLayout());
		this.setMinimumSize(new Dimension(PANEL_WIDTH,PANEL_HEIGHT));
		this.setPreferredSize(new Dimension(PANEL_WIDTH,PANEL_HEIGHT));
		this.setBorder(new LineBorder(Color.BLACK));
		this.add(topPanel,BorderLayout.NORTH);
		
		this.add(botPanel,BorderLayout.SOUTH);
		this.add(leftPanel,BorderLayout.WEST);
		this.add(rightPanel,BorderLayout.EAST);
		this.add(midPanel,BorderLayout.CENTER);
		midPanel.setLayout(new GridLayout(4,2));
		midPanel.add(new JLabel("value:"));
		midPanel.add(valueLabel);
		midPanel.add(new JLabel("noise:"));
		midPanel.add(noiseField);
		midPanel.add(new JLabel("reward:"));
		midPanel.add(rewardField);
		midPanel.add(new JLabel("start:"));
		midPanel.add(rb);
		midPanel.setBackground(Color.WHITE);
		formatPanels();
	}

	private void formatPanels() {
		topPanel.setMinimumSize(new Dimension(100,PANEL_THICKNESS));
		topPanel.setPreferredSize(new Dimension(100,PANEL_THICKNESS));
		botPanel.setMinimumSize(new Dimension(100,PANEL_THICKNESS));
		botPanel.setPreferredSize(new Dimension(100,PANEL_THICKNESS));
		leftPanel.setMinimumSize(new Dimension(PANEL_THICKNESS,PANEL_HEIGHT-2*PANEL_THICKNESS));
		leftPanel.setPreferredSize(new Dimension(PANEL_THICKNESS,PANEL_HEIGHT-2*PANEL_THICKNESS));
		rightPanel.setMinimumSize(new Dimension(PANEL_THICKNESS,PANEL_HEIGHT-2*PANEL_THICKNESS));
		rightPanel.setPreferredSize(new Dimension(PANEL_THICKNESS,PANEL_HEIGHT-2*PANEL_THICKNESS));
		midPanel.setMinimumSize(new Dimension(PANEL_WIDTH-2*PANEL_THICKNESS,PANEL_HEIGHT-2*PANEL_THICKNESS));
		midPanel.setPreferredSize(new Dimension(PANEL_WIDTH-2*PANEL_THICKNESS,PANEL_HEIGHT-2*PANEL_THICKNESS));
		
		
		topPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
		botPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
		leftPanel.setLayout(new BorderLayout());
		rightPanel.setLayout(new BorderLayout());
		
		topLabel.setFont(topLabel.getFont().deriveFont(PANEL_THICKNESS));
		botLabel.setFont(topLabel.getFont().deriveFont(PANEL_THICKNESS));
		leftLabel.setFont(topLabel.getFont().deriveFont(PANEL_THICKNESS));
		rightLabel.setFont(topLabel.getFont().deriveFont(PANEL_THICKNESS));
		
		topPanel.add(topLabel);
		botPanel.add(botLabel);
		leftPanel.add(leftLabel,BorderLayout.CENTER);
		rightPanel.add(rightLabel,BorderLayout.CENTER);
		
//		topPanel.setBorder(new LineBorder(Color.RED));
//		botPanel.setBorder(new LineBorder(Color.RED));
//		leftPanel.setBorder(new LineBorder(Color.RED));
//		rightPanel.setBorder(new LineBorder(Color.RED));
//		midPanel.setBorder(new LineBorder(Color.GREEN));
		
	}
	
	private void setValuesToFields(){
		rewardField.setText(""+reward);
		valueLabel.setText(""+value);
		noiseField.setText(""+noise);
	}
	
	private void getValuesFromFields(){
		try{
			reward = Double.parseDouble(rewardField.getText());
		}catch(Exception e){
			e.printStackTrace();
		}
		
		try{
			noise = Double.parseDouble(noiseField.getText());
		}catch(Exception e){
			e.printStackTrace();
		}
		
		
	}
	
	private String getArrow(int i){
		String string = "▲";
		Color color = new Color(100, 200, 20);
		topPanel.setBackground(Color.LIGHT_GRAY);
		botPanel.setBackground(Color.LIGHT_GRAY);
		leftPanel.setBackground(Color.LIGHT_GRAY);
		rightPanel.setBackground(Color.LIGHT_GRAY);
		
		switch(i){
			
			case 1:
				string= ""+UP;
				topLabel.setText(string);
				topPanel.setBackground(color);
				botLabel.setText("");
				leftLabel.setText("");
				rightLabel.setText("");
				break;
			case 2:
				string=""+DOWN;
				topLabel.setText("");
				botLabel.setText(string);
				botPanel.setBackground(color);
				leftLabel.setText("");
				rightLabel.setText("");
				break;
			case 3:
				string=""+LEFT;
				topLabel.setText("");
				botLabel.setText("");
				leftLabel.setText(string);
				leftPanel.setBackground(color);
				rightLabel.setText("");
				break;
			case 4:
				string=""+RIGHT;
				topLabel.setText("");
				botLabel.setText("");
				leftLabel.setText("");
				rightLabel.setText(string);
				rightPanel.setBackground(color);
				break;
			case 5:
				string="stay";
				topLabel.setText(""+DOWN);
				botLabel.setText(""+UP);
				leftLabel.setText(""+RIGHT);
				rightLabel.setText(""+LEFT);
				topPanel.setBackground(color);
				botPanel.setBackground(color);
				leftPanel.setBackground(color);
				rightPanel.setBackground(color);
			default:
				string="☺";
		}
		
		return string;
	}
	
	

	public int getNextAction() {
		return nextAction;
	}

	public void setNextAction(int nextAction) {
		this.nextAction = nextAction;
		getArrow(nextAction);
	}

	public double getValue() {
		getValuesFromFields();
		return value;
	}

	public void setValue(double value) {
		this.value = value;
		setValuesToFields();
	}

	public double getNoise() {
		getValuesFromFields();
		return noise;
	}

	public void setNoise(double noise) {
		this.noise = noise;
		setValuesToFields();
	}

	public double getReward() {
		getValuesFromFields();
		return reward;
	}

	public void setReward(double reward) {
		this.reward = reward;
		setValuesToFields();
	}
	
	

}
